﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TerenXNA
{
    public class CoordinateConverter
    {
        // szerokośći geograficzne
        double latitude;
        float latDegrees; //stopnie
        float latMinutes; //minuty
        float latSeconds; //sekundy
        double latMeter; //ile stopni przypada na jeden metr
        double latOffset;

        // długości geograficzne
        double longitude;
        float longDegrees; //stopnie
        float longMinutes; //minuty
        float longSeconds; //sekundy
        double longMeter; //ile stopni przypada na jeden metr
        double longOffset;

        private void initialize()
        {
            // przeliczniki
            /*
            latMeter = 0.00000901693;
            longMeter = 0.00001556287;

            latOffset = 10000 * 54 + 100 * 83.33333333 + 13.333333;
            latOffset /= 10000;
            longOffset = 10000 * 18 + 100 * 36.66666667 + 25;
            longOffset /= 10000;
            */
            latOffset = 54.833635;
            longOffset = 18.363223;

            latMeter = (54.833635 - 54.244848) / 65540.121;
            longMeter = (19.332233 - 18.363223) / 60998.637;
        }

        public int[] meters2degrees(double x, double y)
        {
            initialize();
            int[] results = new int[6];

            latitude = latOffset - x * latMeter * 1.0425;
            latDegrees = (int)latitude;
            latitude -= (int)latitude;
            latitude *= 100;
            latitude *= 0.6;
            latMinutes = (float)latitude;
            latitude -= (int)latitude;
            latitude *= 100;
            latitude *= 0.6;
            latSeconds = (float)latitude;

            while (latSeconds < 0)
            {
                latSeconds += 100;
                latMinutes -= 1;
                Console.WriteLine("latMinutes--");
            }

            while (latMinutes < 0)
            {
                latMinutes += 100;
                latDegrees -= 1;
            }

            longitude = longOffset + y * longMeter * 0.9145;
            longDegrees = (int)longitude;
            longitude -= (int)longitude;
            longitude *= 100;
            longitude *= 0.6;
            longMinutes = (float)longitude;
            longitude -= (int)longitude;
            longitude *= 100;
            longitude *= 0.6;
            longSeconds = (float)longitude;

            while (longSeconds >= 100)
            {
                longSeconds -= 100;
                longMinutes += 1;
                Console.WriteLine("longMinutes++");
            }

            while (longMinutes >= 100)
            {
                longMinutes -= 100;
                longDegrees += 1;
            }

            latMinutes = (int)latMinutes;

            results[0] = (int)latDegrees;
            results[1] = (int)latMinutes;
            results[2] = (int)latSeconds;
            results[3] = (int)longDegrees;
            results[4] = (int)longMinutes;
            results[5] = (int)longSeconds;

            return results;
        }

        public Vector3 degrees2meters(double lat, double lon)
        {
            initialize();
            Vector3 result = new Vector3();

            latitude = latOffset - lat;
            //latitude /= (latMeter * 1.0425);
            latitude *= 65540.121 * 0.9847;
            latitude *= 1.5943;

            result.X = (float)latitude;

            longitude = lon - longOffset;
            //longitude /= (longMeter * 0.9145);
            longitude *= 60998.637 * 0.9765;
            longitude *= 1.1699;
            result.Y = (float)longitude;

            return result;
        }

    }
}
